<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams" size="small" label-width="84px">
        <!-- <el-form-item label="类型名称" :label-width="labelWidth" prop="typeName">
          <el-input v-model="queryParams.typeName" placeholder="请输入类型名称" clearable></el-input>
        </el-form-item> -->
        <el-form-item label="工单类型" :label-width="labelWidth" prop="status">
          <el-select v-model="queryParams.typeNo" placeholder="请选择工单类型" clearable>
            <el-option
              v-for="item in workOrderTypeData"
              :key="item.typeNo"
              :label="item.typeName"
              :value="item.typeNo"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button v-if="checkAuthority('/select')" type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="handleAdd" type="primary" icon="plus" size="small">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column prop="typeNo" label="工单类型编号" width="200px"></el-table-column>
        <el-table-column prop="typeName" label="类型名称" width="300px"></el-table-column>
        <!-- <el-table-column prop="remark" label="应用场景"></el-table-column> -->
        <el-table-column prop="typeRemark" label="类型备注"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
            <!-- <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button> -->
            <el-button @click="handleView(scope.row)" type="text" size="small">查看关联工单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total,prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>
    <!-- 编辑弹出框 -->
    <work-order-type-modal ref="WorkOrderTypeModal" @ok="modalOk"></work-order-type-modal>
    <relation-work-order-modal ref="RelationWorkOrderModal"></relation-work-order-modal>
  </section>
</template>

<script>
import Vue from 'vue'
import { getWorkOrderTypeList, deleteWorkOrder, getTypeList } from './js/api'
import WorkOrderTypeModal from './modules/WorkOrderTypeModal'
import RelationWorkOrderModal from './modules/RelationWorkOrderModal'
import { USER_INFO } from '@/views/children/workOrder/js/constants'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限
export default {
  components: { WorkOrderTypeModal, RelationWorkOrderModal },
  data () {
    return {
      labelWidth: '70px',
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      queryParams: {},
      pageNum: 1,
      pageSize: 10,
      totalPageNum: 10,
      tableData: [],
      dialogFormVisible: true,
      workOrderTypeData: []
    }
  },
  mounted () {
    this.getWorkOrderTypeList()
    this.getTypeList()
  },

  methods: {
    checkAuthority,
    formatStatus (row, column, cellValue) {
      return this.$base.constFormatter(cellValue)
    },
    // 工单类型列表
    async getTypeList () {
      let res = await getTypeList(this.tenantNo)
      this.workOrderTypeData = res.data
    },
    onSubmit () {
      this.pageNum = 1
      this.getWorkOrderTypeList()
    },
    async getWorkOrderTypeList () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      const data = Object.assign({}, this.queryParams, params)
      let res = await getWorkOrderTypeList(data)
      this.tableData = res.data.list
      this.totalPageNum = res.data.total
    },
    // 新增
    handleAdd () {
      this.$refs.WorkOrderTypeModal.isAdd = true
      this.$refs.WorkOrderTypeModal.add()
    },
    // 新增
    handleEdit (row) {
      this.$refs.WorkOrderTypeModal.isAdd = false
      this.$refs.WorkOrderTypeModal.edit(row)
    },
    // 查看
    handleView (row) {
      this.$refs.RelationWorkOrderModal.edit(row)
    },
    // 删除
    handleDelete (row) {
      this.$confirm('您确定删除当前类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteFun(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async deleteFun (row) {
      let res = await deleteWorkOrder(row.typeNo)
      if (res.code === 0) {
        if (this.tableData.length === 1 && this.pageNum > 1) {
          this.pageNum--
        }
        this.$message.success('删除成功!')
        this.getWorkOrderTypeList()
      } else {
        this.$message.error(res.msg)
      }
    },
    // 分页
    pageHandleChange (page) {
      this.pageNum = page
      this.getWorkOrderTypeList()
    },
    // 回调
    modalOk () {
      this.getWorkOrderTypeList()
      this.getTypeList()
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .header-card .el-card__body {
  padding-bottom: 4px;
}
.table-card {
  .el-pagination {
    text-align: center;
    display: block;
    margin: 10px auto;
  }
  /deep/ .el-table th {
    display: table-cell !important;
  }
}
.el-dialog {
  .el-select {
    width: 100%;
  }
}
/deep/ .table-btn {
  margin-bottom: 15px;
}
</style>
